package com.pkk.blny.web.controller.api;

import com.pkk.blny.model.domain.Post;
import com.pkk.blny.model.dto.JsonResult;
import com.pkk.blny.model.enums.PostTypeEnum;
import com.pkk.blny.model.enums.ResponseStatusEnum;
import com.pkk.blny.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <pre>
 *     页面API
 * </pre>
 *
 * @author : RYAN0UP
 * @date : 2018/6/6
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/api/pages")
public class ApiPageController {

  @Autowired
  private PostService postService;

  /**
   * 获取单个页面
   *
   * <p>
   * result json:
   * <pre>
   * {
   *     "code": 200,
   *     "msg": "OK",
   *     "result": {
   *         "postId": ,
   *         "user": {},
   *         "postTitle": "",
   *         "postType": "",
   *         "postContentMd": "",
   *         "postContent": "",
   *         "postUrl": "",
   *         "postSummary": ,
   *         "categories": [],
   *         "tags": [],
   *         "comments": [],
   *         "postThumbnail": "",
   *         "postDate": "",
   *         "postUpdate": "",
   *         "postStatus": 0,
   *         "postViews": 0,
   *         "allowComment": 1,
   *         "customTpl": ""
   *     }
   * }
   *     </pre>
   * </p>
   *
   * @param postId postId
   * @return JsonResult
   */
  @GetMapping(value = "/{postId}")
  public JsonResult pages(@PathVariable(value = "postId") Long postId) {
    Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_PAGE.getDesc());
    if (null != post) {
      postService.cacheViews(post.getPostId());
      return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
    } else {
      return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());
    }
  }
}
